using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 改变场景特效
    /// 参数列表1：1：实体tag（0表示自己，-1表示不依赖实体），2：特效id，3：是否是添加
    /// </summary>
    public class ExecChangeSceneEffect : BaseServiceExecution
    {
        public override void Execute(LevelRegion region, LevelPlayer player)
        {
            var config = _config as ExecParamChangeSceneEffect;
            if (!config.tagOrMcid.TryGetValue(triggerEntity, out int tagOrMcid))
            {
                LogicLog.LogError("无法获取到tagOrMcid.");
                return;
            }
            int effectCid = config.effectCid;
            bool isAdd = config.isAdd;
            LevelEntity entity = null;
            if (tagOrMcid == 0)
            {
                entity = triggerEntity;
            }
            else if (tagOrMcid > 0)
            {
                entity = region.GetEntityByTagOrMcid(tagOrMcid);
            }

            if (tagOrMcid >= 0 && entity == null)
            {
                LogicLog.LogError($"改变场景特效执行错误。找不到实体，tagOrMcid:{tagOrMcid}");
                return;
            }

            if (effectCid <= 0 || TableCenter.levelSceneEffect.Get(effectCid) == null)
            {
                LogicLog.LogError($"改变场景特效执行错误。特效id无效，effectCid:{effectCid}");
                return;
            }

            if (tagOrMcid >= 0)
            {
                entity.ChangeSceneEffect(effectCid, isAdd);
            }
            else
            {
                region.ChangeSceneEffect(effectCid, isAdd);
            }

            region.NotifyChangeSceneEffect(player, entity, effectCid, isAdd);
        }
    }
}
